package logic

import (
	"context"

	"server_1.0/service/admin/admin"
	"server_1.0/service/admin/internal/svc"
	"server_1.0/service/admin/model"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *admin.UpdateReq) (*admin.QReps, error) {
	newAd := model.Admin{
		Username: in.No,
		Password: in.Password,
	}
	usersHandle := make([]*admin.Reps, 0)
	count := 0
	err := l.svcCtx.AdminModel.Update(l.ctx, &newAd)
	if err != nil {
		usersHandle = append(
			usersHandle,
			&admin.Reps{
				Code:     int64(util.ChangeError),
				No:       "not found",
				Password: "unknown",
			})
		count += 1
		return &admin.QReps{
			Count: int64(count),
			QRs:   usersHandle,
		}, nil
	}
	usersHandle = append(
		usersHandle,
		&admin.Reps{
			Code:     int64(util.Success),
			No:       newAd.Username,
			Password: newAd.Password,
		})
	count += 1
	return &admin.QReps{
		Count: int64(count),
		QRs:   usersHandle,
	}, nil
}
